package ru.lenwp.operation.client;

import ru.lenwp.context.Context;
import ru.lenwp.model.Account;
import ru.lenwp.model.Client;
import ru.lenwp.model.OperationInfo;
import ru.lenwp.operation.Operation;
import ru.lenwp.util.ConsoleUtil;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Lenar on 10.08.2014.
 */
public class RechangeAccount implements Operation {
    private final Context context;

    public RechangeAccount(Context context) {
        this.context = context;
    }

    public void rechangeAccount(Context context) {
        boolean performed = false;
        while (!performed) {
            System.out.println("Какой счет хотите изменить?");
            String acc = ConsoleUtil.askString();
            Account account = null;
            Client client = (Client) context.getCurrentUser();
            for (Account currentAccount : client.getAccountList()) {
                if (currentAccount.getNumber() != null && currentAccount.getNumber().equals(acc)) {
                    account = currentAccount;
                    break;
                }
            }
            if (account != null) {
                double resSum = ConsoleUtil.askDouble("Введите сумму");
                account.setSum((resSum + account.getSum()));
                performed = true;
            } else {
                System.out.println("Указанный номер счета не найден");
            }
        }
    }


    @Override
    public OperationInfo execute() {
        rechangeAccount(context);
        return null;
    }

    @Override
    public String getOperationName() {
        return "Пополнение счета";
    }
}
